<?php

namespace App\Http\Requests\Admin\Question\Question;

use App\Models\Question;
use App\Models\QuestionTag;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $mapType = array_keys(Question::$mapType);
        $mapKeyType = array_keys(Question::$mapKeyType);
        $type = $this->get('type');
        return [
            //
            'type' => ['required', Rule::in($mapType), 'bail'],
            'pid' => [
                function ($attribute, $value, $fail) {
                    if ((int)$value != 0) {
                        $question = Question::where('id', '=', $value)->count('id');
                        if (!$question) {
                            $fail(':attribute不正确');
                        }
                    }
                    return true;
                }, 'bail'],
            'status' => ['required', 'boolean', 'bail'],
            'content' => ['required', 'bail'],
            'options' => [
                Rule::requiredIf($type != Question::TYPE_MASTER),
                'array',
                function ($attribute, $value, $fail) use ($mapKeyType) {
                    $type = $this->get('type');
                    if ($type != Question::TYPE_MASTER) {
                        $count = count($value);
                        if ($count <= 1 || ($type == Question::TYPE_DECIDE && $count > 2)) {
                            $fail(":attribute不正确");
                            return false;
                        }
                        foreach ($value as $v) {
                            if (!isset($v['key']) || !isset($v['content'])) {
                                $fail(":attribute参数错误");
                                return false;
                            }
                            if ($type != Question::TYPE_DECIDE && $type != Question::TYPE_MASTER) {
                                if (!isset($v['type']) || !in_array($v['type'], $mapKeyType)) {
                                    $fail(":attribute必须有个类型");
                                }
                            }
                        }
                        return true;
                    }
                }, 'bail'],
            'keys' => [
                Rule::requiredIf($type != Question::TYPE_MASTER),
                'array',
                function ($attribute, $value, $fail) use ($type) {
                    $count = count($value);
                    if ($type == Question::TYPE_DECIDE) {
                        if ($count != 1) {
                            $fail("判断题的:attribute有且只能有一个");
                            return false;
                        }
                        if (!in_array($value[0], [1, 2])) {
                            $fail("判断题的:attribute只能是正确或者错误");
                        }
                    }
                    if ($type == Question::TYPE_RADIO) {
                        if ($count != 1) {
                            $fail("单选题的:attribute有且只能有一个");
                            return false;
                        }
                    }
                    return true;
                }, 'bail'],
            'analysis' => [Rule::requiredIf($type != Question::TYPE_MASTER), 'bail'],
            'sort' => [Rule::requiredIf($this->get('pid') > 0), 'integer', 'bail'],
            'tag_id' => [
                'array',
                function ($attr, $value, $fail) {
                    $count = count($value);
                    $tag_count = QuestionTag::whereIn('id',$value)->count();
                    if($count==$tag_count){
                        return true;
                    }
                    $fail(':attribute不正确');
                }, 'bail']
        ];
    }

    public function attributes()
    {
        return [
            'type' => '题目类型',
            'pid' => '题帽题',
            'status' => '状态',
            'content' => '题目内容',
            'options' => '选项',
            'keys' => '答案',
            'analysis' => '解析',
            'sort' => '排序',
            'tag_id' => '标签'
        ];
    }
}
